package com.cafebabe.redis.pubsub;

import com.cafebabe.redis.JedisTemplate;
import redis.clients.jedis.JedisPubSub;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by xForMe on 2015/7/22.
 */
public class SubMsg extends JedisPubSub {

    public static ConcurrentHashMap<String,Object> curMsg;
    /**
     * 取得订阅后消息的处理
     * @param chanel
     * @param msg
     */
    @Override
    public void onMessage(String chanel, String msg) {
        addMsg(chanel,msg);
        System.out.println(curMsg.toString());
    }

    /**
     * 取得按表达式方式之后的处理
     * @param s
     * @param s1
     * @param s2
     */
    @Override
    public void onPMessage(String s, String s1, String s2) {
        System.out.println("redis onPMessage " + s);

    }

    /**
     * 初始化订阅之后的处理
     * @param s
     * @param i
     */
    @Override
    public void onSubscribe(String s, int i) {
        System.out.println("redis onSubscribe " + s);
    }

    /**
     * 取消订阅之后的处理
     * @param s
     * @param i
     */
    @Override
    public void onUnsubscribe(String s, int i) {
         System.out.println("redis onUnsubscribe "+s);
    }

    /**
     * 取消按表达式的方式订阅时候的处理
     * @param s
     * @param i
     */
    @Override
    public void onPUnsubscribe(String s, int i) {
         System.out.println("redis onPUnsubscribe "+s);
    }

    /**
     * 初始化按表达式的方式订阅时候的处理
     * @param s
     * @param i
     */
    @Override
    public void onPSubscribe(String s, int i) {
         System.out.println("redis onPSubscribe "+s);
    }

    public static void addMsg(String channel,String msg){
        if(curMsg == null){
            curMsg = new ConcurrentHashMap<String, Object>();
            curMsg.put(channel,msg);
        }else {
            curMsg.put(channel,msg);
        }
    }

}
